<template>
    <div>
        <h3>康复理疗</h3>
        项目名称：<input type="text" v-model="info.Pname">
        <input type="button" value="查询" class="btn btn-info" @click="show">
        <input type="button" value="批量启用" class="btn btn-success" @click="UpdateAll(true)">
        <input type="button" value="批量禁用" class="btn btn-danger" @click="UpdateAll(false)">
        <input type="button" value="导出" class="btn btn-info" @click="outgoing">
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" v-model="checked" @change="checkAll"></td>
                    <td>项目名称</td>
                    <td>项目编号</td>
                    <td>服务时长</td>
                    <td>服务人数</td>
                    <td>佣金</td>
                    <td>项目说明</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
                <tr v-for="i in data.list" :key="i.Id">
                    <td><input type="checkbox" v-model="ids" :value="i.Id" @change="checkOne"></td>
                    <td>{{ i.Pname }}</td>
                    <td>{{ i.CarID }}</td>
                    <td>{{ i.TimeNum }}</td>
                    <td>{{ i.Num }}</td>
                    <td>{{ i.Price }}</td>
                    <td>{{ i.Detail }}</td>
                    <td>{{ i.State==true?"启用":"禁用" }}</td>
                    <td>
                        <input type="button" value="删除" class="btn btn-danger" @click="del(i.Id)">
                    </td>
                </tr>
            </tbody>
        </table>
        <div>
            共<span style="color: red;">{{ data.ListCount }}</span>条
            每页<span style="color: red;">{{ info.PageSize }}</span>条
            共<span style="color: red;">{{ info.PageIndex }}/{{ data.PageCount }}</span>页
            <input type="button" value="首页" @click="Fan(1)">
            <input type="button" value="上一页" @click="Fan(info.PageIndex-1)">
            <input type="button" :value="i" v-for="i in data.PageCount" @click="Fan(i)">
            <input type="button" value="下一页" @click="Fan(info.PageIndex+1)">
            <input type="button" value="尾页" @click="Fan(data.PageCount)">
        </div>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { escapeLeadingUnderscores, flattenDiagnosticMessageText } from 'typescript';
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute();
const routr = useRouter();
onMounted(()=>{
    show();
})

//导出
const outgoing=()=>{
    location.href="https://localhost:7140/api/Food/ExcelOut?PageIndex=1&PageSize=20";
}

let checked:any=ref(false);
let ids:any=ref([]);

//全选
const checkAll=()=>{
    if(checked.value){
        ids.value=data.list.map((i:any)=>{
            return i.Id;
        })
    }else{
        ids.value=[];
    }
}
//反选
const checkOne=()=>{
    if(ids.value.length==data.list.length){
        checked.value=true;
    }else{
        checked.value=false;
    }
}

//批量修改状态
const UpdateAll=(State:any)=>{
    if(ids.value.length==0){
        alert("必须选择一项");
        return;
    }
    axios({
        url:"/api/Food/UpdateStateAll",
        method:"put",
        params:{
            ids:ids.value.toString(),
            State:State
        }
    })
    .then(res => {
        if (res.data > 0) {
            alert("修改状态成功");
            checked.value=false;
            ids.value=0;
            show();
        } else {
            alert("修改状态失败");
            checked.value=false;
            ids.value=0;
            return;
        }
    })
    .catch(err => {
        console.log(err);
    })
}

//删除
const del=(id:any)=>{
    if(confirm("确认删除吗？")){
        axios({
            url:"/api/Food/DelPTS",
            method:"delete",
            params:{id:id}
        })
        .then(res => {
            if (res.data > 0) {
                alert("删除成功");
                show();
            } else {
                alert("删除失败");
                return;
            }
        })
        .catch(err => {
            console.log(err);
        })
    }
}

//翻页
const Fan=(num:any)=>{
    if(num<1){
        info.PageIndex=1;
        return;
    }
    if(num>data.PageCount){
        info.PageIndex=data.PageCount;
        return;
    }
    info.PageIndex=num;
    show();
}

//显示
let data: any = reactive({
    list:[],
    ListCount:0,
    PageCount:0
});
let info: any = reactive({
    PageIndex:1,
    PageSize:2,
    Pname: "",
})
const show=()=>{
    axios({
        url:"/api/Food/ShowPTS",
        method:"get",
        params:info
    })
    .then(res=>{
        Object.assign(data,res.data);
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped></style>